<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/4/23  9:56
 */
namespace app\admin\Controller;
use think\Controller;

class Set extends Common
{
    public function __construct()
    {
        parent::__construct();
    }



//	版权
    public function copyright()
    {
        if(request()->isPost())
        {
            $data['copyright']=input('copyright');
            $res = db('web_set')->where('id', 1)->update($data);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('copyright')->where('id',1)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 轮播图
     */
    public function banner()
    {
        $banner = db('banner');
        $ban = $banner->select();
        $this -> assign("res",$ban);
        return $this->fetch();
    }
    /**
     * 轮播添加或修改
     */
    public function banner_edit()
    {
        $id = input('id');
        if(request()->isPost()){
            $data['status'] = input('post.status');
            $data['title'] = input('post.title');
            $data['displayorder'] = input('post.displayorder');
            $data['url'] = input('post.url');
            $file = request()->file('bnimage');
            if (!empty($file)) {
                $info = $file->move(ROOT_PATH . 'static/uploads');
                if ($info) {
                    $data['pic'] = $info->getSaveName();
                } else {
                    $this->success($file->getError());
                }
            } else {
                $data['pic'] = input('bne');
            }
            if($id != null){
                $bnr = db('banner')->where("id='$id'")->update($data);
            }else{
                $bnr = db('banner')->insert($data);
            }
            $this->success("操作成功!",url('Set/banner'));
        }
        $banner=db('banner')->where("id='$id'")->find();
        $this -> assign("banner",$banner);
        return $this->fetch();

    }
    /**
     * 轮播删除
     */
    public function banner_del()
    {
        $bnid = input('id');
        $banner = db('banner');
        $del = $banner -> where("id='$bnid'") -> delete();
        if($del)
        {
            $this ->success("删除成功",url('Set/banner'));
        }

    }

    /**
     * 关于我们
     */
    public function about()
    {
        if(request()->isAjax())
        {
            $data['content']=input('content');
            $data['title']=input('title');
            $img = input('imagename');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH .  'static' . DS . 'uploads');
                if($info)
                {
                    $data['image']= $info->getSaveName();
                }
            }
            if($img){
                $pic = ROOT_PATH . 'static/uploads/'.$img;
                $dir =  dirname($pic);
                @unlink($pic);
                if(is_dir($dir)){
                    @rmdir($dir);
                }
            }
            $da['about'] = serialize($data);
            $res = db('web_set')->where('id', 1)->update($da);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('about')->where('id',1)->find();
//        halt($data);
        $data = unserialize($data['about']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 联系我们
     */
    public function contact()
    {
        if(request()->isPost())
        {
            $data['hotline'] = input('hotline');
            $data['email'] = input('email');
            $data['hq'] = input('hq');
            $data['hqfax'] = input('hqfax');
            $data['hqphone'] = input('hqphone');
            $data['shop'] = input('shop');
            $data['sfax'] = input('sfax');
            $data['sphone'] = input('sphone');
            $data['area'] = input('area');

            $da['contact'] = serialize($data);
//            halt($res);
            $res = db('web_set')->where('id',1)->update($da);
            if($res)
            {
                $this->success('操作成功！');
            }
        }

        $data = db('web_set')->where('id', 1)->field('contact')->find();
        $data = unserialize($data['contact']);
//        halt($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 加入我们
     */
    public function join()
    {
        if(request()->isPost())
        {
            $data['content']=input('content');
            $data['title']=input('title');
//            $data['des']=input('des');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads');
                if($info)
                {
                    $data['image']= $info->getSaveName();
                }
            }
            $da['join'] = serialize($data);
            $res = db('web_set')->where('id', 1)->update($da);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('join')->where('id',1)->find();
//        halt($data);
        $data = unserialize($data['join']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 招商简介
     */

    public function investment()
    {
        if(request()->isPost())
        {
            $data['content']=input('content');
            $data['title']=input('title');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads');
                if($info)
                {
                    $data['image']= $info->getSaveName();
                }
                //修改删除图片
                $da = db('web_set')->field('investment')->where('id',1)->find();
                $da = unserialize($da['investment']);
                $pic = ROOT_PATH . 'static/uploads/'.$da['image'];
                unlink($pic);

            }
            $da['investment'] = serialize($data);
            $res = db('web_set')->where('id', 1)->update($da);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('investment')->where('id',1)->find();
//        halt($data);
        $data = unserialize($data['investment']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 文化简介
     */

    public function culture()
    {
        if(request()->isPost())
        {
            $data['content']=input('content');
            $data['title']=input('title');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH .'static' . DS . 'uploads');
                if($info)
                {
                    $data['image']= $info->getSaveName();
                }
            }
            $da['culture'] = serialize($data);
            $res = db('web_set')->where('id', 1)->update($da);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('culture')->where('id',1)->find();
//        halt($data);
        $data = unserialize($data['culture']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 责任简介
     */

    public function duty()
    {
        if(request()->isPost())
        {
            $data['content']=input('content');
            $data['title']=input('title');
            $file=request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads');
                if($info)
                {
                    $data['image']= $info->getSaveName();
                }
            }
            $da['duty'] = serialize($data);
            $res = db('web_set')->where('id', 1)->update($da);

            if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
        }
        $data = db('web_set')->field('duty')->where('id',1)->find();
//        halt($data);
        $data = unserialize($data['duty']);
        $this->assign('data', $data);
        return $this->fetch();
    }
}
